﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.IO;
using BLL;

namespace WebApplication.Admins
{
    public partial class Upload : System.Web.UI.Page
    {
        public string id = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"] ?? "";
            if (!IsPostBack)
                Display();
            

        }
        private void Display()
        {
            int pageSize = GlobalConfiguration.ImagesPerPage;
            string imageFolder = MapPath("~/TinTucImages");
            DirectoryInfo dir = new DirectoryInfo(imageFolder);
            FileInfo[] allFiles = dir.GetFiles();
     
            int totalFiles = allFiles.Length;

            string page = Request.QueryString["Page"] ?? "1";
            int howManyPages = (int)Math.Ceiling((double)totalFiles / (double)pageSize);
            int currentPage = int.Parse(page);

            int skip = (currentPage - 1) * pageSize;
            FileInfo[] files = allFiles.OrderByDescending(x => x.CreationTime)
                        .Skip(skip).Take(pageSize).ToArray();

            dlImage.DataSource = files;
            dlImage.DataBind();
            string firstPageUrl = LinkBLL.ToAdminUpLoad(id);
            string pagerUrl = LinkBLL.ToAdminUpLoad(id, "{0}");
            pagerTop.Show(currentPage, howManyPages, firstPageUrl, pagerUrl, false);
            pagerBottom.Show(currentPage, howManyPages, firstPageUrl, pagerUrl, true);
        }
        bool CheckFileType(string fileName)
        {
            string ext = Path.GetExtension(fileName);
            switch (ext.ToLower())
            {
                case ".gif":
                    return true;
                case ".png":
                    return true;
                case ".jpg":
                    return true;
                case ".jpeg":
                    return true;
                default:
                    return false;
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            try
            {

                if (upImage.HasFile && upImage.PostedFile.ContentLength <= 500000)
                {

                    if (CheckFileType(upImage.FileName))
                    {

                        String filePath = "~/TinTucImages/" + upImage.FileName;


                        upImage.SaveAs(MapPath(filePath));

                        lblStatus.Text = "Upload successful";
                        Display();
                    }
                }
                else
                {
                    lblStatus.Text = "Uploading image failed";
                }

            }
            catch (Exception)
            {
                lblStatus.Text = "Uploading image failed";
            }
        }
    }
}